/**
 * 通用页面组件初始化
 * select2
 * icheck
 * laydate
 * Copyright (c) 2019 ruoyi 
 */
$(function () {
    // select2复选框事件绑定
    if ($.fn.select2 !== undefined) {
        $("select.form-control:not(.noselect2)").each(function () {
            $(this).select2().on("change", function () {
                $(this).valid();
            })
        })
    }
    // checkbox 事件绑定
    if ($(".check-box").length > 0) {
        $(".check-box").iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
//            increaseArea: '20%' // optional
        })
    }
    // radio 事件绑定
    if ($(".radio-box").length > 0) {
        $(".radio-box").iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
//            increaseArea: '20%' // optional
        })
    }
    // laydate 时间控件绑定
    if ($(".select-time").length > 0) {
        var com = window.laydate;
        var startDate = com.render({
            elem: '#startTime',
            max: $('#endTime').val(),
            theme: 'molv',
            trigger: 'click',
            done: function (value, date) {
                // 结束时间大于开始时间
                if (value !== '') {
                    endDate.config.min.year = date.year;
                    endDate.config.min.month = date.month - 1;
                    endDate.config.min.date = date.date;
                } else {
                    endDate.config.min.year = '';
                    endDate.config.min.month = '';
                    endDate.config.min.date = '';
                }
            }
        });
        var endDate = com.render({
            elem: '#endTime',
            min: $('#startTime').val(),
            theme: 'molv',
            trigger: 'click',
            done: function (value, date) {
                // 开始时间小于结束时间
                if (value !== '') {
                    startDate.config.max.year = date.year;
                    startDate.config.max.month = date.month - 1;
                    startDate.config.max.date = date.date;
                } else {
                    startDate.config.max.year = '';
                    startDate.config.max.month = '';
                    startDate.config.max.date = '';
                }
            }
        });
    }
    // laydate time-input 时间控件绑定
    if ($(".time-input").length > 0) {

        var com = window.laydate;
        $(".time-input").each(function (index, item) {
            var time = $(item);
            // 控制控件外观
            var type = time.attr("data-type") || 'date';
            // 控制回显格式
            var format = time.attr("data-format") || 'yyyy-MM-dd';
            // 控制日期控件按钮
            var buttons = time.attr("data-btn") || 'clear|now|confirm', newBtnArr = [];
            // 日期控件选择完成后回调处理
            var callback = time.attr("data-callback") || {};
            if (buttons) {
                if (buttons.indexOf("|") > 0) {
                    var btnArr = buttons.split("|"), btnLen = btnArr.length;
                    for (var j = 0; j < btnLen; j++) {
                        if ("clear" === btnArr[j] || "now" === btnArr[j] || "confirm" === btnArr[j]) {
                            newBtnArr.push(btnArr[j]);
                        }
                    }
                } else {
                    if ("clear" === buttons || "now" === buttons || "confirm" === buttons) {
                        newBtnArr.push(buttons);
                    }
                }
            } else {
                newBtnArr = ['clear', 'now', 'confirm'];
            }
            com.render({
                elem: item,
                theme: 'molv',
                trigger: 'click',
                type: type,
                format: format,
                btns: newBtnArr,
                done: function (value, data) {
                    if (typeof window[callback] != 'undefined'
                            && window[callback] instanceof Function) {
                        window[callback](value, data);
                    }
                }
            });
        });

        ;
    }

});
/** 设置全局ajax处理 */
$.ajaxSetup({
    complete: function (XMLHttpRequest, textStatus) {
        if (textStatus == 'timeout') {
            $.alertMsg.alertWarning("服务器超时，请稍后再试！");
//            $.alertMsg.closeLoading();
        } else if (textStatus == "parsererror") {
            $.alertMsg.alertWarning("服务器错误，请联系管理员！");
//            $.alertMsg.closeLoading();
        }
    }
});
$.validator.setDefaults({
    highlight: function (element) {
        $(element).closest('.form-group').find(".form-control:first").addClass('is-invalid');
    },
    unhighlight: function (element) {
        $(element).closest('.form-group').find(".form-control:first").removeClass('is-invalid');
        $(element).closest('.form-group').find(".form-control:first").addClass('is-valid');
    },
    errorElement: 'em',
    errorClass: 'invalid-feedback',
    submitHandler: function (form) {
        alert("提交事件,全局设置!");
    }
});
web_status = {
    SUCCESS: 200,
    FAIL: 500,
    WARNING: 301
};